import { createSlice } from '@reduxjs/toolkit';
import axios  from 'axios';
const channelStore = createSlice({
    name:'channel',
    //初始化state
    initialState:{
        channelList:[]
    },
    //修改状态的方法  同步方法,  支持直接修改
    reducers:{
        setChannels(state,action){
            state.channelList = action.payload
        },
    }
})

//结构出来的方法 actionCreater函数
const { setChannels } = channelStore.actions

//异步请求方法
const fetchChannels = ()=>{
    return async (dispatch)=>{
      const res = await axios.get('http://geek.itheima.net/v1_0/channels')
        console.log(res)
        dispatch(setChannels(res.data.data.channels))
    }
}

//导出reducer
const reducer = channelStore.reducer

export { fetchChannels }

//导出reducer
export default reducer

